<template>
  <div ref="rootRef" class="slider">
    <div class="slider-group">
      <div v-for="item in sliders" :key="item.id" class="slider-page">
        <a :href="item.link"><img :src="item.pic" /></a>
      </div>
    </div>
    <div class="dots-wrapper">
      <span
        v-for="(item, index) in sliders"
        :key="item.id"
        :class="{ active: currentPageIndex === index }"
        class="dot"
      ></span>
    </div>
  </div>
</template>

<script>
/**
 * 轮播图组件
 */
import { ref } from 'vue'
import useSlider from '@/components/base/slider/use-slider'

export default {
  name: 'slider',
  props: {
    sliders: {
      type: Array,
      default () {
        return []
      }
    }
  },
  setup () {
    const rootRef = ref(null)
    const { currentPageIndex } = useSlider(rootRef)
    return { rootRef, currentPageIndex }
  }
}
</script>

<style lang="scss" scoped>
.slider {
  position: relative;

  .slider-group {
    display: flex;
    overflow: scroll;

    .slider-page {
      width: 100%;
      //防止压缩
      flex-shrink: 0;

      img {
        width: 100%;
      }
    }
  }

  .dots-wrapper {
    position: absolute;
    left: 50%;
    bottom: 12px;
    transform: translateX(-50%);
    display: flex;

    .dot {
      height: 8px;
      width: 8px;
      margin: 0 4px;
      border-radius: 50%;
      background: $color-text-l;

      &.active {
        width: 20px;
        border-radius: 5px;
        background: $color-text-ll;
      }
    }
  }
}
</style>
